﻿using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Serialization;

namespace AirXBMC
{
    public static class XmlSerialization
    {
        public static void SerializeObjectToXmlFile<T>(string filename, T obj)
        {
            var xmlSerializer = new XmlSerializer(typeof(T));
            var writer = XmlWriter.Create(filename, new XmlWriterSettings { Encoding = Encoding.UTF8, Indent = true });
            xmlSerializer.Serialize(writer, obj);
            writer.Close();
        }

        public static T DeserializeXmlFileToObject<T>(string filename)
        {
            using (var fileStream = new FileStream(filename, FileMode.Open))
            {
                var xmlSerializer = new XmlSerializer(typeof(T));
                var xmlTextWriter = new XmlTextWriter(fileStream, Encoding.UTF8);

                return (T)xmlSerializer.Deserialize(xmlTextWriter.BaseStream);
            }
        }
    }
}
